import 'package:azlistview/azlistview.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:sports_new/controller/match/list/match_page_logic.dart';
import 'package:sports_new/controller/match/list/zq_end_logic.dart';
import 'package:sports_new/controller/match/list/zq_focus_logic.dart';
import 'package:sports_new/controller/match/list/zq_notbegin_logic.dart';
import 'package:sports_new/controller/match/list/zq_view_logic.dart';
import 'package:sports_new/controller/service/config_service.dart';
import 'package:sports_new/controller/service/match_service.dart';
import 'package:sports_new/entity/match/hot_league_list_entity.dart';
import 'package:sports_new/entity/match/zq_list_entity.dart';
import 'package:sports_new/utils/constant.dart';
import 'package:sports_new/utils/sp_utils.dart';

enum ZqMatchType { hot,focus, all, begin, schedule, end }

enum ZqFilterType { all, jz, dc, ssc, yj }

class ZqMatchFilterLogic extends GetxController {
  ZqMatchFilterLogic({this.hotLabel});
  String? hotLabel;
  //全部数据源
  List<IndexLeagueEntity> indexLeagueEntity = [];
  //全部
  List<LeagueEntity> leagueList = [];
  List<int> hideLeagueId = [];
  List<int> allLeagueId = [];
  //一级
  List<LeagueEntity> yjLeagueList = [];
  List<int> yjLeagueId = [];
  List<int> yjHideLeagueId = [];
  //竞足
  List<LeagueEntity> jzLeagueList = [];
  List<int> jzLeagueId = [];
  List<int> jzHideLeagueId = [];
  //单场
  List<LeagueEntity> dcLeagueList = [];
  List<int> dcLeagueId = [];
  List<int> dcHideLeagueId = [];
  //十四场
  List<LeagueEntity> sscLeagueList = [];
  List<int> sscLeagueId = [];
  List<int> sscHideLeagueId = [];
  // HotLeagueListEntity? oneLevelData;
  List<int> oneLevelId = Get.find<MatchService>().zqOneLevelId;
  List<int> hotId = Get.find<MatchService>().zqHotId;
  List<int> hideMatchId = [];
  var hideMatchNum = 0;
  var type = ZqMatchType.all;
  var leagueType = ZqFilterType.all;
  //比赛频道右下角需要显示的类型 0全部1一级2竞足
  QuickFilter quickFilter = QuickFilter.all;


  void initLeague(ZqMatchType type) {
    this.type = type;
    if ((type == ZqMatchType.schedule ||
            type == ZqMatchType.end ||
            type == ZqMatchType.begin) &&
        Get.find<ConfigService>().zqConfig.zqAlert7 == 1) {
      quickFilter =
          Get.find<ZqViewLogic>(tag: Constant.matchFilterTagAll).quickFilter;
      if (quickFilter == QuickFilter.jingcai) {
        leagueType = ZqFilterType.jz;
      } else if (quickFilter == QuickFilter.yiji) {
        leagueType = ZqFilterType.all;
      }
    }
    if (allLeagueId.isEmpty) {
      getAllLeague();
      loadLocalData();
      if (quickFilter == QuickFilter.yiji) {
        onSelectOneLevel();
      } else {
        updateLeague();
      }

      // if (matchType == MatchType.all) {}
    } else {
      getAllLeague();
      updateLeague();
    }
  }

  void clearLeague() {
    // leagueType = LeagueType.all;
    hideLeagueId = [];
    allLeagueId = [];
    yjLeagueId = [];
    jzHideLeagueId = [];
    dcHideLeagueId = [];
    sscHideLeagueId = [];
    hideMatchNum = 0;
  }

  void getAllLeague() {
    // type = matchType;
    allLeagueId = [];
    jzLeagueId = [];
    yjLeagueId = [];
    dcLeagueId = [];
    sscLeagueId = [];
    // int hideMatchTemp = 0;
    List<LeagueEntity> leagueList = [];
    List<ZqListEntity> data = [];
    if(type == ZqMatchType.hot){
      data =
          Get.find<ZqViewLogic>(tag: "${Constant.matchFilterTagHot}$hotLabel").originalMatch;
    }else if (type == ZqMatchType.all) {
      data =
          Get.find<ZqViewLogic>(tag: Constant.matchFilterTagAll).originalMatch;
    } else if (type == ZqMatchType.begin) {
      data = Get.find<ZqViewLogic>(tag: Constant.matchFilterTagBegin)
          .originalMatch;
    } else if (type == ZqMatchType.schedule) {
      final index = Get.find<ZqNotBeginLogic>().index;
      data =
          Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagSchedule}$index')
              .originalMatch;
    } else if (type == ZqMatchType.end) {
      final index = Get.find<ZqEndLogic>().index;
      data =
          Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagResult}$index')
              .originalMatch;
    } else if (type == ZqMatchType.focus) {
      data = Get.find<ZqFocusLogic>().originalMatch;
    }

    for (var match in data) {
      if (match.leagueId == null) {
        continue;
      }
      List<LeagueEntity> hasLeague = leagueList
          .where((element) => element.leagueId == match.leagueId)
          .toList();
      if (hasLeague.isEmpty) {
        final league = LeagueEntity(
            leagueId: match.leagueId, leagueName: match.leagueName);
        // ..isSelected = !hideLeagueId.contains(match.leagueId);
        league.matchNum = 1;
        leagueList.add(league);
      } else {
        hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
      }
      if (!allLeagueId.contains(match.leagueId)) {
        allLeagueId.add(match.leagueId!);
      }
      if (oneLevelId.contains(match.leagueId) &&
          !yjLeagueId.contains(match.leagueId)) {
        yjLeagueId.add(match.leagueId!);
      }
      if (match.jcMatchNo != null && !jzLeagueId.contains(match.leagueId)) {
        jzLeagueId.add(match.leagueId!);
      }
      if (match.dcMatchNo != null && !dcLeagueId.contains(match.leagueId)) {
        dcLeagueId.add(match.leagueId!);
      }
      if (match.sfcMatchNo != null && !sscLeagueId.contains(match.leagueId)) {
        sscLeagueId.add(match.leagueId!);
      }
    }
    handleList(leagueList);
  }

  //点击tabbar或底部按钮
  void updateLeague() {
    // leagueType = type;
    int hideMatchTemp = 0;
    List<LeagueEntity> leagueListTemp = [];
    List<ZqListEntity> data = [];
    if(type == ZqMatchType.hot){
      data =
          Get.find<ZqViewLogic>(tag: "${Constant.matchFilterTagHot}$hotLabel").originalMatch;
    } else if (type == ZqMatchType.all) {
      data =
          Get.find<ZqViewLogic>(tag: Constant.matchFilterTagAll).originalMatch;
    } else if (type == ZqMatchType.begin) {
      data = Get.find<ZqViewLogic>(tag: Constant.matchFilterTagBegin)
          .originalMatch;
    } else if (type == ZqMatchType.schedule) {
      final index = Get.find<ZqNotBeginLogic>().index;
      data =
          Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagSchedule}$index')
              .originalMatch;
    } else if (type == ZqMatchType.end) {
      final index = Get.find<ZqEndLogic>().index;
      data =
          Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagResult}$index')
              .originalMatch;
    } else if (type == ZqMatchType.focus) {
      data = Get.find<ZqFocusLogic>().originalMatch;
    }

    for (var match in data) {
      if (match.leagueId != null) {
        final league = LeagueEntity(
            leagueId: match.leagueId, leagueName: match.leagueName);
        if (leagueType == ZqFilterType.all) {
          if (hideLeagueId.contains(league.leagueId)) {
            league.isSelected = false;
            hideMatchTemp++;
          } else {
            league.isSelected = true;
          }
          List<LeagueEntity> hasLeague = leagueListTemp
              .where((element) => element.leagueId == match.leagueId)
              .toList();
          if (hasLeague.isEmpty) {
            league.matchNum = 1;
            leagueListTemp.add(league);
          } else {
            hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
          }
        } else if (leagueType == ZqFilterType.yj) {
          if (yjLeagueId.contains(league.leagueId)) {
            if (yjHideLeagueId.contains(match.leagueId)) {
              league.isSelected = false;
              hideMatchTemp++;
            } else {
              league.isSelected = true;
            }
            List<LeagueEntity> hasLeague = leagueListTemp
                .where((element) => element.leagueId == match.leagueId)
                .toList();
            if (hasLeague.isEmpty) {
              league.matchNum = 1;
              leagueListTemp.add(league);
            } else {
              hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
            }
          } else {
            hideMatchTemp++;
          }
        } else if (leagueType == ZqFilterType.jz) {
          if (jzLeagueId.contains(league.leagueId)) {
            if (jzHideLeagueId.contains(match.leagueId)) {
              league.isSelected = false;
              hideMatchTemp++;
            } else {
              league.isSelected = true;
              if (match.jcMatchNo == null) {
                hideMatchTemp++;
                hideMatchId.add(match.id ?? 0);
                continue;
              }
            }
            List<LeagueEntity> hasLeague = leagueListTemp
                .where((element) => element.leagueId == match.leagueId)
                .toList();
            if (hasLeague.isEmpty) {
              league.matchNum = 1;
              leagueListTemp.add(league);
            } else {
              hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
            }
          } else {
            hideMatchTemp++;
          }
        } else if (leagueType == ZqFilterType.dc) {
          if (dcLeagueId.contains(league.leagueId)) {
            if (dcHideLeagueId.contains(match.leagueId)) {
              league.isSelected = false;
              hideMatchTemp++;
            } else {
              league.isSelected = true;
              if (match.dcMatchNo == null) {
                hideMatchTemp++;
                hideMatchId.add(match.id ?? 0);
                continue;
              }
            }
            List<LeagueEntity> hasLeague = leagueListTemp
                .where((element) => element.leagueId == match.leagueId)
                .toList();
            if (hasLeague.isEmpty) {
              league.matchNum = 1;
              leagueListTemp.add(league);
            } else {
              hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
            }
          } else {
            hideMatchTemp++;
          }
        } else if(leagueType == ZqFilterType.ssc){
          if (sscLeagueId.contains(league.leagueId)) {
            if (sscHideLeagueId.contains(match.leagueId)) {
              league.isSelected = false;
              hideMatchTemp++;
            } else {
              league.isSelected = true;
              if (match.sfcMatchNo == null) {
                hideMatchTemp++;
                hideMatchId.add(match.id ?? 0);
                continue;
              }
            }
            List<LeagueEntity> hasLeague = leagueListTemp
                .where((element) => element.leagueId == match.leagueId)
                .toList();
            if (hasLeague.isEmpty) {
              league.matchNum = 1;
              leagueListTemp.add(league);
            } else {
              hasLeague.first.matchNum = hasLeague.first.matchNum! + 1;
            }
          } else {
            hideMatchTemp++;
          }
        }
      }
    }
    hideMatchNum = hideMatchTemp;
    if (leagueType == ZqFilterType.all) {
      leagueList = leagueListTemp;
      handleList(leagueListTemp);
    } else {
      if (leagueType == ZqFilterType.yj) {
        yjLeagueList = leagueListTemp;
      } else if (leagueType == ZqFilterType.jz) {
        jzLeagueList = leagueListTemp;
      } else if (leagueType == ZqFilterType.dc) {
        dcLeagueList = leagueListTemp;
      } else if (leagueType == ZqFilterType.ssc) {
        sscLeagueList = leagueListTemp;
      }
      // hideMatchNum = hideMatchTemp;
      update();
    }
  }

  handleList(List<LeagueEntity> list) {
    // List<LeagueEntity> list = data.level1List!;
    List<LeagueEntity> hotLeague = [];
    indexLeagueEntity = [];
    for (int i = 0, length = list.length; i < length; i++) {
      if (hotId.contains(list[i].leagueId)) {
        //热门比赛
        hotLeague.add(list[i]);
      } else {
        String pinyin = PinyinHelper.getPinyinE(list[i].leagueName!);
        String tag = pinyin.substring(0, 1).toUpperCase();
        bool hasTag = false;
        for (var match in indexLeagueEntity) {
          if (match.tagIndex == tag) {
            match.leagues.add(list[i]);
            hasTag = true;
            continue;
          }
        }
        if (!hasTag) {
          indexLeagueEntity.add(IndexLeagueEntity([list[i]], tag));
        }
      }
    }

    SuspensionUtil.sortListBySuspensionTag(indexLeagueEntity);
    if (hotLeague.isNotEmpty) {
      indexLeagueEntity.insert(0, IndexLeagueEntity(hotLeague, '★'));
    }
    SuspensionUtil.setShowSuspensionStatus(indexLeagueEntity);
    update();
  }

  void onTapLeague(LeagueEntity league) {
    league.isSelected = !league.isSelected;
    if (leagueType == ZqFilterType.all) {
      if (!league.isSelected && !hideLeagueId.contains(league.leagueId)) {
        hideLeagueId.add(league.leagueId!);
      } else {
        hideLeagueId.remove(league.leagueId);
      }
    } else if (leagueType == ZqFilterType.yj) {
      if (!league.isSelected && !yjHideLeagueId.contains(league.leagueId)) {
        yjHideLeagueId.add(league.leagueId!);
      } else {
        yjHideLeagueId.remove(league.leagueId);
      }
    } else if (leagueType == ZqFilterType.jz) {
      if (!league.isSelected && !jzHideLeagueId.contains(league.leagueId)) {
        jzHideLeagueId.add(league.leagueId!);
      } else {
        jzHideLeagueId.remove(league.leagueId);
      }
    } else if (leagueType == ZqFilterType.dc) {
      if (!league.isSelected && !dcHideLeagueId.contains(league.leagueId)) {
        dcHideLeagueId.add(league.leagueId!);
      } else {
        dcHideLeagueId.remove(league.leagueId);
      }
    } else if(leagueType == ZqFilterType.ssc){
      if (!league.isSelected && !sscHideLeagueId.contains(league.leagueId)) {
        sscHideLeagueId.add(league.leagueId!);
      } else {
        sscHideLeagueId.remove(league.leagueId);
      }
    }
    updateLeague();
  }

  List<LeagueEntity> getLeagueList(ZqFilterType type) {
    List<LeagueEntity> list = [];
    if (type == ZqFilterType.jz) {
      list = jzLeagueList;
    } else if (type == ZqFilterType.yj) {
      list = yjLeagueList;
    } else if (type == ZqFilterType.dc) {
      list = dcLeagueList;
    } else if (type == ZqFilterType.ssc) {
      list = sscLeagueList;
    }
    return list;
  }

  void onSelectFilterType(ZqFilterType type) {
    leagueType = type;
    // setMatchListType(type == LeagueType.jz ? 2 : 0);
    updateQuickFilterType();
    updateLeague();
  }

  void onSelectOneLevel() {
    quickFilter = QuickFilter.yiji;
    yjHideLeagueId = [];
    List<int> hideLeagueIdTemp = [];
    if (leagueType == ZqFilterType.jz) {
      for (var leagueId in jzLeagueId) {
        if (!oneLevelId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      jzHideLeagueId = hideLeagueIdTemp;
    } else if (leagueType == ZqFilterType.dc) {
      for (var leagueId in dcLeagueId) {
        if (!oneLevelId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      dcHideLeagueId = hideLeagueIdTemp;
    } else if (leagueType == ZqFilterType.ssc) {
      for (var leagueId in sscLeagueId) {
        if (!oneLevelId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      sscHideLeagueId = hideLeagueIdTemp;
    } else{
      for (var leagueId in allLeagueId) {
        if (!oneLevelId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      hideLeagueId = hideLeagueIdTemp;
    }
    updateLeague();
  }

  void onSelectAll() {
    if (leagueType == ZqFilterType.all) {
      hideLeagueId = [];
    } else if (leagueType == ZqFilterType.yj) {
      yjHideLeagueId = [];
    } else if (leagueType == ZqFilterType.jz) {
      jzHideLeagueId = [];
    } else if (leagueType == ZqFilterType.dc) {
      dcHideLeagueId = [];
    } else {
      sscHideLeagueId = [];
    }
    updateQuickFilterType();
    updateLeague();
  }

  void onSelectReverse() {
    List<int> hideLeagueIdTemp = [];
    if (leagueType == ZqFilterType.all) {
      for (var leagueId in allLeagueId) {
        if (!hideLeagueId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      hideLeagueId = hideLeagueIdTemp;
    } else if (leagueType == ZqFilterType.jz) {
      for (var leagueId in jzLeagueId) {
        if (!jzHideLeagueId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      jzHideLeagueId = hideLeagueIdTemp;
    } else if (leagueType == ZqFilterType.yj) {
      for (var leagueId in yjLeagueId) {
        if (!yjHideLeagueId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      yjHideLeagueId = hideLeagueIdTemp;
    } else if (leagueType == ZqFilterType.dc) {
      for (var leagueId in dcLeagueId) {
        if (!dcHideLeagueId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      dcHideLeagueId = hideLeagueIdTemp;
    } else {
      for (var leagueId in sscLeagueId) {
        if (!sscHideLeagueId.contains(leagueId)) {
          hideLeagueIdTemp.add(leagueId);
        }
      }
      sscHideLeagueId = hideLeagueIdTemp;
    }
    updateQuickFilterType();
    updateLeague();
  }

  void onConfirm() {
    if(!(leagueType == ZqFilterType.all && quickFilter == QuickFilter.yiji) && leagueType != ZqFilterType.jz){
      quickFilter = QuickFilter.all;
    }
    List<int> hideLeagueIdList = getHideLeagueIdList();
    savaFilterToLocal();
    if(type == ZqMatchType.hot){
      Get.find<ZqViewLogic>(tag: "${Constant.matchFilterTagHot}$hotLabel")
          .onSelectMatchType(quickFilter, fromFilterPage: true);
      Get.find<ZqViewLogic>(tag: "${Constant.matchFilterTagHot}$hotLabel")
          .onSelectMatchType(quickFilter, fromFilterPage: true);
    }else if (type == ZqMatchType.all) {
      Get.find<ZqViewLogic>(tag: Constant.matchFilterTagAll)
          .onSelectMatchType(quickFilter, fromFilterPage: true);
      Get.find<ZqViewLogic>(tag: Constant.matchFilterTagAll)
          .filterMatch(hideLeagueIdList);
    } else if (type == ZqMatchType.begin) {
      Get.find<ZqViewLogic>(tag: Constant.matchFilterTagBegin).quickFilter =
          quickFilter;
      Get.find<ZqViewLogic>(tag: Constant.matchFilterTagBegin)
          .filterMatch(hideLeagueIdList);
    } else if (type == ZqMatchType.schedule) {
      final index = Get.find<ZqNotBeginLogic>().index;
      Get.find<ZqNotBeginLogic>()
          .selectMatchType(quickFilter, fromFilterPage: true);
      Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagSchedule}$index')
          .filterMatch(hideLeagueIdList);
    } else if (type == ZqMatchType.end) {
      final index = Get.find<ZqEndLogic>().index;
      Get.find<ZqEndLogic>().selectMatchType(quickFilter, fromFilterPage: true);
      Get.find<ZqViewLogic>(tag: '${Constant.matchFilterTagResult}$index')
          .filterMatch(hideLeagueIdList);
    } else if (type == ZqMatchType.focus) {
      Get.find<ZqFocusLogic>().quickFilter = quickFilter;
      Get.find<ZqFocusLogic>().filterMatch(hideLeagueIdList);
    }
    navigator?.pop();
  }

  List<int> getHideLeagueIdList() {
    List<int> hideLeagueIdList = List.from(allLeagueId);
    if (leagueType == ZqFilterType.all) {
      hideLeagueIdList = hideLeagueId;
    } else if (leagueType == ZqFilterType.yj) {
      List<int> selectLeague = yjLeagueId
          .where((element) => !yjHideLeagueId.contains(element))
          .toList();
      for (var e in selectLeague) {
        hideLeagueIdList.remove(e);
      }
    } else if (leagueType == ZqFilterType.jz) {
      List<int> selectLeague = jzLeagueId
          .where((element) => !jzHideLeagueId.contains(element))
          .toList();
      for (var e in selectLeague) {
        hideLeagueIdList.remove(e);
      }
    } else if (leagueType == ZqFilterType.dc) {
      List<int> selectLeague = dcLeagueId
          .where((element) => !dcHideLeagueId.contains(element))
          .toList();
      for (var e in selectLeague) {
        hideLeagueIdList.remove(e);
      }
    } else if(leagueType == ZqFilterType.ssc){
      List<int> selectLeague = sscLeagueId
          .where((element) => !sscHideLeagueId.contains(element))
          .toList();
      for (var e in selectLeague) {
        hideLeagueIdList.remove(e);
      }
    }
    return hideLeagueIdList;
  }

  void updateQuickFilterType() {
    if (leagueType == ZqFilterType.jz) {
      quickFilter = QuickFilter.jingcai;
    } else if (leagueType == ZqFilterType.yj) {
      quickFilter = QuickFilter.yiji;
    } else {
      quickFilter = QuickFilter.all;
    }
  }

  //只保存全部频道
  void savaFilterToLocal() {
    if (type == ZqMatchType.all) {
      List<int> hideLeague = [];
      if (leagueType == ZqFilterType.all) {
        hideLeague = hideLeagueId;
      } else if (leagueType == ZqFilterType.yj) {
        hideLeague = yjHideLeagueId;
      } else if (leagueType == ZqFilterType.jz) {
        hideLeague = jzHideLeagueId;
      } else if (leagueType == ZqFilterType.dc) {
        hideLeague = dcHideLeagueId;
      } else if(leagueType == ZqFilterType.ssc){
        hideLeague = sscHideLeagueId;
      }
      final json = {
        'hideLeagueId': hideLeague,
        // 'type': type.index,
        'leagueType': leagueType.index,
        'matchType': QuickFilter.values.indexOf(quickFilter)
      };
      SpUtils.matchAllFilterData = json;
    }
  }

  void loadLocalData() {
    if (type == ZqMatchType.all) {
      final filterJson = SpUtils.matchAllFilterData;
      if (filterJson != null) {
        leagueType = ZqFilterType.values[filterJson['leagueType']];
        quickFilter = QuickFilter.values[filterJson['matchType']];
        final hideLeague = filterJson['hideLeagueId'].cast<int>();
        if (leagueType == ZqFilterType.all) {
          hideLeagueId = hideLeague;
        } else if (leagueType == ZqFilterType.yj) {
          yjHideLeagueId = hideLeague;
        } else if (leagueType == ZqFilterType.jz) {
          jzHideLeagueId = hideLeague;
        } else if (leagueType == ZqFilterType.dc) {
          dcHideLeagueId = hideLeague;
        } else {
          sscHideLeagueId = hideLeague;
        }
        updateLeague();
      }
    }
  }
}

class IndexLeagueEntity extends ISuspensionBean {
  List<LeagueEntity> leagues;
  String tagIndex;
  IndexLeagueEntity(this.leagues, this.tagIndex);
  @override
  String getSuspensionTag() {
    return tagIndex;
  }
}
